package cn.sccl.dirms.monitor.service.impl;

import cn.sccl.common.database.DataBase;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.model.QueryData;
import cn.sccl.common.util.DateUtil;
import cn.sccl.dirms.monitor.model.AuditLog;
import cn.sccl.dirms.monitor.service.AuditLogManager;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author _minus
 * @create 2017-04-25 11:25
 */
@Service
public class AuditLogManagerImpl implements AuditLogManager{
    @Autowired
    DataBase dataBase ;

    @Override
    public List<AuditLog> findAll() {
        String sql ="SELECT\n" +
                "	l.*, u.alias as alias \n" +
                "FROM\n" +
                "	auditlog l,\n" +
                "	users u\n" +
                "WHERE\n" +
                "	1 = 1\n" +
                "AND u.userid = l.userid\n" +
                "ORDER BY\n" +
                "	l.clock DESC";
        return  dataBase.query4Model(sql, AuditLog.class);
    }

    @Override
    public PageResult<AuditLog> pageQueryAuditLogs(long pageIndex, int pageSize, QueryData queryData) {
        String sql ="SELECT\n" +
                "	l.*, u.alias as alias \n" +
                "FROM\n" +
                "	auditlog l,\n" +
                "	users u\n" +
                "WHERE\n" +
                "	1 = 1\n" +
                "AND u.userid = l.userid\n" ;
        String timeRange = MapUtils.getString(queryData,"timeRange");
        String timeFormatter = MapUtils.getString(queryData,"timeFormatter","yyyy-MM-dd");
        String keywords = MapUtils.getString(queryData,"keywords");
        Integer resource = MapUtils.getInteger(queryData,"resource");
        Integer action = MapUtils.getInteger(queryData,"action");
        Date startD = null;
        Date endD = null;
        if(!StringUtils.isEmpty(timeRange)){
            String start = timeRange.split(",")[0];
            String end = timeRange.split(",")[1];
            try {
                startD =  DateUtils.parseDate(start,timeFormatter);
            } catch (ParseException e) {

            }
            try {
                endD =  DateUtils.parseDate(end,timeFormatter);
            } catch (ParseException e) {

            }
        }
        List<Object> params = new ArrayList<>();
        if(startD != null && endD != null){
            sql += " and (l.clock between ? and ? ) ";
            params.add(startD.getTime()/1000);
            Calendar c = Calendar.getInstance();
            c.setTime(endD);
            c.add(Calendar.HOUR_OF_DAY,24);
            c.add(Calendar.MILLISECOND,-1);
            params.add(c.getTime().getTime()/1000);
        }
        if(!StringUtils.isEmpty(keywords)){
            sql += " and (u.alias like ? or l.ip like ? or l.resourcename like ? or l.details like ? or l.resourceid = ?) ";
            params.add("%"+keywords+"%");
            params.add("%"+keywords+"%");
            params.add("%"+keywords+"%");
            params.add("%"+keywords+"%");
            params.add("%"+keywords+"%");
        }
        if(resource != null){
            sql += " and l.resourcetype = ? ";
            params.add(resource);
        }

        if(action != null){
            sql += " and l.action = ? ";
            params.add(action);
        }
        sql += " ORDER BY l.clock DESC ";
        return dataBase.query4Model(sql,pageIndex,pageSize, AuditLog.class,params.toArray());
    }
}
